package bug1

import (
	"context"

	"github.com/gogf/gf/v2/frame/g"
)

func Bug1() {
	// 方式1，在传入**类型时，未按照orm tag进行匹配
	var entity *GfTest1
	err := g.DB().Model("gf_test1").Where("test_id", "123").Scan(&entity)
	// OUTPUT: 2021-11-15 10:12:39.528 [DEBU] [  1 ms] [default] [rows:1  ] SELECT `id` FROM `gf_test1` WHERE `test_id`='123' LIMIT 1
	if err != nil {
		panic(err)
	}
	g.Log().Info(context.Background(), entity)
	// 2021-11-15 10:12:39.528 [INFO] {"Id":1,"TestNum":"","TestMingCheng":""}

	// 方式2 ，可以按照 orm tag 进行匹配
	var entity1 GfTest1
	err = g.DB().Model("gf_test1").Where("test_id", "123").Scan(&entity1)
	// 2021-11-15 10:12:39.530 [DEBU] [  1 ms] [default] [rows:1  ] SELECT `id`,`test_id`,`test_name` FROM `gf_test1` WHERE `test_id`='123' LIMIT 1
	if err != nil {
		panic(err)
	}
	g.Log().Info(context.Background(), entity1)

	// 方式3 ，可以按照 orm tag 进行匹配
	var entity2 = GfTest1{}
	err = g.DB().Model("gf_test1").Where("test_id", "123").Scan(&entity2)
	// 2021-11-15 10:12:39.530 [DEBU] [  1 ms] [default] [rows:1  ] SELECT `id`,`test_id`,`test_name` FROM `gf_test1` WHERE `test_id`='123' LIMIT 1
	if err != nil {
		panic(err)
	}
	g.Log().Info(context.Background(), entity2)
	// 原因 github.com/gogf/gf/v2/internal/utils  IsStruct 方法判断时缺少对 **nil类型的判断
	// for reflectKind == reflect.Ptr {
	// 	if !reflectValue.IsValid() || reflectValue.IsNil() {
	// 		reflectValue = reflect.New(reflectValue.Type().Elem()).Elem()
	// 		reflectKind = reflectValue.Kind()
	// 	} else {
	// 		reflectValue = reflectValue.Elem()
	// 		reflectKind = reflectValue.Kind()
	// 	}
	// }

	// 在按照方式2和方式3测试的时候，在数据库里没有数据时，会返回error sql: no rows in result set
}

func Bug2() {
	// 通过视图查询, 视图bug，
	// mysql测试后能够正常获取视图的字段，
	// sqlserver无法获取视图的字段

	var entity *GfTest2
	err := g.DB().Model("v_gf_test1").Where("test_id", "123").Scan(&entity)
	//2021-11-15 10:38:01.789 [DEBU] [  2 ms] [default] [rows:3  ] SHOW FULL COLUMNS FROM `v_gf_test1`
	//2021-11-15 10:38:01.790 [DEBU] [  0 ms] [default] [rows:1  ] SELECT `id`,`test_id`,`test_name` FROM `v_gf_test1` WHERE `test_id`='123' LIMIT 1
	if err != nil {
		panic(err)
	}
	g.Log().Info(context.Background(), entity)

	var entity1 *GfTest2
	err = g.DB("sqlserver").Model("v_gf_test1").Where("test_id", "123").Scan(&entity1)
	//2021-11-15 10:38:01.790 [INFO] {"Id":1,"TestId":"123","TestName":"测试123"}
	// 2021-11-15 10:38:01.980 [DEBU] [187 ms] [sqlserver] [rows:0  ] SELECT a.name Field, CASE b.name WHEN 'datetime' THEN 'datetime' WHEN 'numeric' THEN b.name + '(' + convert(varchar(20),
	// a.xprec) + ',' + convert(varchar(20), a.xscale) + ')' WHEN 'char' THEN b.name + '(' + convert(varchar(20), a.length)+ ')' WHEN 'varchar' THEN b.name + '(' + convert(varchar(20), a.length)+ ')' ELSE b.name + '(' + convert(varchar(20),a.length)+ ')' END AS Type, CASE WHEN a.isnullable=1 THEN 'YES' ELSE 'NO' end AS [Null], CASE WHEN exists ( SELECT 1 FROM sysobjects WHERE xtype='PK' AND name IN ( SELECT name FROM sysindexes WHERE indid IN ( SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid ) ) ) THEN 'PRI' ELSE '' END AS [Key], CASE WHEN COLUMNPROPERTY(a.id,a.name,'IsIdentity')=1 THEN 'auto_increment' ELSE '' END Extra, isnull(e.text,'') AS [Default], isnull(g.[value],'') AS [Comment] FROM syscolumns a LEFT JOIN systypes b ON a.xtype=b.xtype AND a.xusertype=b.xusertype INNER JOIN sysobjects d ON a.id=d.id AND d.xtype='U' AND d.name<>'dtproperties' LEFT JOIN syscomments e ON a.cdefault=e.id LEFT
	// JOIN sys.extended_properties g ON a.id=g.major_id AND a.colid=g.minor_id LEFT JOIN sys.extended_properties f ON d.id=f.major_id AND f.minor_id =0 WHERE d.name='V_GF_TEST1' ORDER BY a.id,a.colorder
	// 2021-11-15 10:38:01.999 [ERRO] [ 14 ms] [sqlserver] [rows:0  ] SELECT TOP 1  Id,TestId,TestName FROM v_gf_test1 WHERE test_id='123'

	// 解决方案，把 d.xtype='U' 这个条件删除， 在Model是视图的时候 d.xtype='V'
	if err != nil {
		//Error: mssql: 列名 'TestName' 无效。
		panic(err)
	}
	g.Log().Info(context.Background(), entity1)
}
